package com.ycx.ycxmybatismanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ycx.ycxmybatismanagement.dto.AddEmpDTO;
import com.ycx.ycxmybatismanagement.dto.EmpExprListDTO;
import com.ycx.ycxmybatismanagement.dto.EmpListDTO;
import com.ycx.ycxmybatismanagement.dto.UpdateEmpDTO;
import com.ycx.ycxmybatismanagement.entity.EmpLog;
import com.ycx.ycxmybatismanagement.exception.BizException;
import com.ycx.ycxmybatismanagement.mapper.EmpExprMapper;
import com.ycx.ycxmybatismanagement.mapper.EmpMapper;
import com.ycx.ycxmybatismanagement.pojo.Emp;
import com.ycx.ycxmybatismanagement.pojo.EmpExpr;
import com.ycx.ycxmybatismanagement.pojo.PageBean;
import com.ycx.ycxmybatismanagement.service.EmpService;
import com.ycx.ycxmybatismanagement.vo.EmpListVo;
import com.ycx.ycxmybatismanagement.vo.EmpVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    /**
     * 新分页
     * @param empListDTO
     * @return
     */
    @Override
    public PageBean<EmpListVo> empPageList(EmpListDTO empListDTO) {
        //开启分页
        PageHelper.startPage(empListDTO.getPage(),empListDTO.getPageSize());
        Page<EmpListVo> empList = empMapper.selectPageHelper(empListDTO);
        return new PageBean(empList.getTotal(),empList.getResult());
    }

    /**
     * 添加
     * @param empDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEmp(AddEmpDTO empDTO) {
        boolean success =false;
        Emp empDB =  empMapper.selectUsername(empDTO.getUsername());
        if (empDB!=null){
            throw new BizException("用户名重复");
        }
        empDB = empMapper.selectPhone(empDTO.getPhone());
        if (empDB!=null){
            throw new BizException("手机号重复");
        }
        try {
            Emp emp = new Emp();
            BeanUtils.copyProperties(empDTO,emp);
            emp.setPassword("123456");
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.addEmp(emp);
            List<EmpExprListDTO> exprListDTO = empDTO.getExprList();
            if (!CollectionUtils.isEmpty(exprListDTO)){
                List<EmpExpr> empExprList = new ArrayList<>();
                //遍历进行类型转换
                for (EmpExprListDTO empExprListDTO : exprListDTO) {
                    EmpExpr expr = new EmpExpr();
                    BeanUtils.copyProperties(empExprListDTO, expr);
                    expr.setEmpId(emp.getId());
                    empExprList.add(expr);
                }
                empExprMapper.addEmpExpr(empExprList);
            }

        } catch (Exception e) {
             success =false;
            throw new RuntimeException(e);
        } finally {
            // 记录日志
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("添加员工"+(success?"成功":"失败"));
//            logService.insertLog(empLog);

        }


    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmps(List<Integer> ids) {
        empMapper.deleteEmpsByIds(ids);
        empExprMapper.deleteEmpExprByIds(ids);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @Override
    public EmpVO selectById(Integer id) {
        EmpVO empVO = new EmpVO();
        Emp emp =  empMapper.selectEmpById(id);
        List<EmpExpr> exprList = empExprMapper.selectExprByEmpId(id);
        BeanUtils.copyProperties(emp,empVO);
        empVO.setExprList(exprList);
        return empVO;
    }

    /**
     * 更新
     * @param empDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(UpdateEmpDTO empDTO) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(empDTO,emp);
        emp.setUpdateTime(LocalDateTime.now());
        //修改
        empMapper.updateEmp(emp);

        Integer id = empDTO.getId();
        List<Integer> empIds = Arrays.asList(id);
        //通过员工id删除员工经历表的数据
        empExprMapper.deleteEmpExprByIds(empIds);
        List<EmpExprListDTO> exprListDTO = empDTO.getExprList();
        //非空则类型转换
        if (!CollectionUtils.isEmpty(exprListDTO)){

            List<EmpExpr> empExprList = exprListDTO.stream().map(exprDTO -> {
                EmpExpr empExpr = new EmpExpr();
                BeanUtils.copyProperties(exprDTO, empExpr);
                empExpr.setEmpId(id);
                return empExpr;
            }).collect(Collectors.toList());
            empExprMapper.addEmpExpr(empExprList);
        }

    }

    @Override
    public List<EmpListVo> empsList() {
        List<EmpListVo> empListVos = empMapper.selectEmpList();
        return empListVos;
    }


    /**
     * 旧分页
     * @param empListDTO
     * @return
     */
//    public PageBean<EmpListVo> empPageList1(EmpListDTO empListDTO) {
//        //查询总条数
//       int count = empMapper.countEmp();
//        //当前页列表
//        int start = (empListDTO.getPage()-1)*empListDTO.getPageSize();
//        List<EmpListVo> empPageList = empMapper.selectPage(start,empListDTO.getPageSize());
//        return new PageBean(Long.valueOf(count),empPageList);
//    }
    



}
